package cn.iocoder.yudao.module.wms.dal.mysql.stockdetail;

import java.math.BigDecimal;
import java.util.*;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.module.wms.dal.dataobject.stockdetail.StockDetailDO;
import org.apache.ibatis.annotations.Mapper;
import cn.iocoder.yudao.module.wms.controller.admin.stockdetail.vo.*;
import org.apache.ibatis.annotations.Update;

/**
 * 库存明细 Mapper
 *
 * @author 芋道源码
 */
@Mapper
public interface StockDetailMapper extends BaseMapperX<StockDetailDO> {

    default PageResult<StockDetailDO> selectPage(StockDetailPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<StockDetailDO>()
                .eqIfPresent(StockDetailDO::getProductId, reqVO.getProductId())
                .eqIfPresent(StockDetailDO::getWarehouseId, reqVO.getWarehouseId())
                .eqIfPresent(StockDetailDO::getPositionId, reqVO.getPositionId())
                .eqIfPresent(StockDetailDO::getTotalStockQty, reqVO.getTotalStockQty())
                .eqIfPresent(StockDetailDO::getStockQty, reqVO.getStockQty())
                .eqIfPresent(StockDetailDO::getUnitAmt, reqVO.getUnitAmt())
                .eqIfPresent(StockDetailDO::getCostAmt, reqVO.getCostAmt())
                .eqIfPresent(StockDetailDO::getBatchNo, reqVO.getBatchNo())
                .eqIfPresent(StockDetailDO::getSerialNo, reqVO.getSerialNo())
                .eqIfPresent(StockDetailDO::getDocId, reqVO.getDocId())
                .eqIfPresent(StockDetailDO::getDocCode, reqVO.getDocCode())
                .betweenIfPresent(StockDetailDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(StockDetailDO::getId));
    }

    @Update("UPDATE wms_stock_detail SET stock_qty = stock_qty + #{qty} WHERE id = #{id}")
    void updateStockDetail(Long id, BigDecimal qty);

    default List<StockDetailDO> selectList(Long productId, Long warehouseId, Long positionId) {
        return selectList(new LambdaQueryWrapperX<StockDetailDO>()
                .eqIfPresent(StockDetailDO::getProductId, productId)
                .eqIfPresent(StockDetailDO::getWarehouseId, warehouseId)
                .eqIfPresent(StockDetailDO::getPositionId, positionId));
    }

    default List<StockDetailDO> selectList(Long docId, String docCode) {
        return selectList(new LambdaQueryWrapperX<StockDetailDO>()
                .eqIfPresent(StockDetailDO::getDocId, docId)
                .eqIfPresent(StockDetailDO::getDocCode, docCode));
    }
}